
    /**  
    * @Title: AllTypeSort.java
    * @Package com.util.sort
    * @Description: 
    * @author likai
    * @date 2016年8月31日
    * @version V1.0  
    */
    
package com.utils.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
    * @ClassName: AllTypeSort
    * @Description: 各种类型的排序
    * @author likai
    * @date 2016年8月31日
    *
    */

public class AllTypeSort {
	
	public static void main(String[] args) {
		Map<String, String> map=new HashMap<String, String>();
		map.put("gmt_payment", "qqqq");
		map.put("buyer_id", "ssss");
		System.out.println(MapSortByKey(map));
	}
	
	/**
	 * map按键进行的ASCII 码从小到大排序（字典序）后，将键值对按key1=value1&key2=value2…拼接
	 * @param parameters
	 * @return
	 * @throws Exception 
	 */
	public static String MapSortByKey(Map<String, String> parameters){
		String buff = "";
		List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(parameters.entrySet());
		//按键排序
		Collections.sort(infoIds,
				new Comparator<Map.Entry<String, String>>() {
					public int compare(Map.Entry<String, String> o1,Map.Entry<String, String> o2) {
						return (o1.getKey()).toString().compareTo(o2.getKey());
					}
				});

		//键值对拼接
		for (int i = 0; i < infoIds.size(); i++) {
			Map.Entry<String, String> item = infoIds.get(i);
			if (item.getKey() != "") {
				buff += item.getKey() + "="
						+item.getValue() + "&";
			}
		}
		if (buff.isEmpty() == false) {
			buff = buff.substring(0, buff.length());
		}

		return buff;
	}

	/**
	 * map按值进行的ASCII 码从小到大排序（字典序）后，将键值对按key1=value1&key2=value2…拼接
	 * @param parameters
	 * @return
	 * @throws Exception 
	 */
	public static String MapSortByValue(HashMap<String, String> parameters)
			throws Exception {

		String buff = "";
		try {
			List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(parameters.entrySet());
			//按键排序
			Collections.sort(infoIds,
					new Comparator<Map.Entry<String, String>>() {
						public int compare(Map.Entry<String, String> o1,Map.Entry<String, String> o2) {
							return (o1.getValue()).toString().compareTo(o1.getValue());
						}
					});

			//键值对拼接
			for (int i = 0; i < infoIds.size(); i++) {
				Map.Entry<String, String> item = infoIds.get(i);
				if (item.getKey() != "") {
					buff += item.getKey() + "="
							+item.getValue() + "&";
				}
			}
			if (buff.isEmpty() == false) {
				buff = buff.substring(0, buff.length());
			}
		} catch (Exception e) {
			throw e;
		}

		return buff;
	}
	
	
	/**
	 * 将map按键从小到大排序
	 *
	 * 
	 * @param list
	 * @param blockSize
	 * @return
	 */
	public static <T> Map<String, T> sortMapByKey(Map<String, T> map) {
		if (map == null || map.isEmpty()) {
			return null;
		}
		Map<String, T> sortMap = new TreeMap<String, T>(new MapKeyComparator());
		sortMap.putAll(map);
		return sortMap;
	}
	
	/**
	 * 将map按键从大到小排序
	 *
	 * 
	 * @param list
	 * @param blockSize
	 * @return
	 */

	public static <T> Map<String, T> sortMapByKeyDesc(Map<String, T> map) {
		if (map == null || map.isEmpty()) {
			return null;
		}
		Map<String, T> sortMap = new TreeMap<String, T>(new MapKeyComparatorDesc());
		sortMap.putAll(map);
		return sortMap;
	}
	

	/**
	 * 将List<String>按值从小到大排序
	 *
	 * 
	 * @param list
	 * @param blockSize
	 * @return
	 */
	public static List<String> sortListByValueString(List<String> list) {
		Collections.sort(list,
				new Comparator<String>() {
					public int compare(String o1,String o2) {
						return o1.compareTo(o2);
					}
				});
		return list;
	}
	
	/**
	 * 将List<String>按值从大到小排序
	 *
	 * 
	 * @param list
	 * @param blockSize
	 * @return
	 */

	public static List<String> sortListByValueStringDesc(List<String> list) {
		Collections.sort(list,
				new Comparator<String>() {
					public int compare(String o1,String o2) {
						return o2.compareTo(o1);
					}
				});
		return list;
	}

	
}
//比较器类
class MapKeyComparator implements Comparator<String> {
	public int compare(String str1, String str2) {
		return str1.compareTo(str2);
	}
}

//比较器类
class MapKeyComparatorDesc implements Comparator<String> {
	public int compare(String str1, String str2) {
		return str2.compareTo(str1);
	}
}